//
//  RCDiscountDetailNewViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-19.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCDiscountDetailNewViewController.h"
#import "RCDiscountDataModel.h"
#import "IManager+ModuleActivityDetail.h"
#import "IManager+ModuleMerchantFavourite.h"
#import "RCActivityDetailDataModel.h"
#import "RCDiscountDetailNewCell.h"
#import "MBProgressHUD.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "NSArray+EX.h"
#import "AppDelegate.h"
#import "NSString+NSString_Ex_.h"
#import "GloableEngineService.h"
#import "RCMerchantDetailWriteSigninViewController.h"
#import "RCMerchantCommitWrongInfoViewController.h"
#import "RCMerchantDetailWriteCommentViewController.h"
#import "RCMerchantUploadPhotoViewController.h"
#import "RCOrderVC.h"
#import "SMPageControl.h"
#import "UIImageView+WebCache.h"


#define kSignImageFile            @"image_merchant_sign.png"
#define kSendPhotoImageFile       @"image_merchant_sendPhoto.png"
#define kDianpingImageFile        @"image_merchant_dianping.png"
#define kWrongImageFile           @"image_merchant_wrong.png"
#define kShareImageFile           @"image_merchant_share.png"
#define kFavouriteImageFile       @"image_merchant_favourite.png"
#define kFavouritedImageFile      @"image_merchant_favourited.png"
#define kBottomBarImageFile       @"image_merchant_bottom_bar.png"

#define kSwitchSceneDotActiveImage     @"image_discountDetail_dot_active.png"
#define kSwitchSceneDotInActiveImage   @"image_discountDetail_dot_inactive.png"
#define kBaomingImageFile              @"image_discountDetail_baoming.png"
#define kLineImageFile                 @"image_order_line.png"

#define kTableViewSectionNum      5

#define kTableCellTablePicture          0
#define kTabelCellTableDiscountInfo     1
#define kTableCellTableShopInfo         2
#define kTableCellTableContentInfo      3
#define kTableCellTableNotice           4

@interface RCDiscountDetailNewViewController ()<UITableViewDataSource,UITableViewDelegate,IManagerDelegate,MBProgressHUDDelegate,RCDiscountHeaderCellDelegate,UIScrollViewDelegate>

@property(nonatomic,retain)UITableView *discountTabelView;
@property(nonatomic,retain)RCActivityDetailDataModel *detailDataModel;
@property(nonatomic,assign)int64_t requestID;
@property (nonatomic, assign) int64_t favouriterRequestID;
@property (nonatomic, assign) int64_t cancelRequestID;
@property (nonatomic, retain) MBProgressHUDEx *progressHub;
@property (nonatomic, copy) NSString *activityID;

@property(nonatomic,retain) UIScrollView *picScrollView;
@property(nonatomic,retain) SMPageControl *picPageControl;
@property(nonatomic,retain)UIView *tableHeaderView;

@property (nonatomic, retain) UIButton *favouriteButton;
@property (nonatomic, assign) BOOL hasFavourited;
@property (nonatomic, assign) NSInteger cellCount;

@end

@implementation RCDiscountDetailNewViewController

@synthesize discountTabelView = _discountTabelView;
@synthesize detailDataModel = _detailDataModel;
@synthesize requestID = _requestID;
@synthesize activityID = _activityID;
@synthesize progressHub = _progressHub;
@synthesize favouriteButton = _favouriteButton;
@synthesize hasFavourited = _hasFavourited;
@synthesize favouriterRequestID = _favouriterRequestID;
@synthesize cancelRequestID = _cancelRequestID;
@synthesize picScrollView = _picScrollView;
@synthesize picPageControl =_picPageControl;
@synthesize tableHeaderView = _tableHeaderView;
@synthesize cellCount = _cellCount;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id) initWithActivityID:(NSString *)activityId
{
    self = [super init];
    if(self){
        self.activityID = activityId;
    }
    return self;
}

-(void)dealloc{
    self.discountTabelView.dataSource = nil;
    self.discountTabelView.delegate = nil;
    self.discountTabelView = nil;
    self.detailDataModel = nil;
    self.activityID = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.picScrollView = nil;
    self.picPageControl = nil;
    self.tableHeaderView = nil;
    self.favouriteButton = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self refreshData];
    self.titleLabelText = @"优惠详情页";
    self.navigationItem.titleView = [self createTitleView];
    
    
    
    CGRect rect = CGRectMake(0, 0, 320, UIScreenHeight-64-46);
    _discountTabelView = [[UITableView alloc] initWithFrame:rect];
    _discountTabelView.backgroundColor = [UIColor clearColor];
    [self.viewContainer addSubview:_discountTabelView];
    _discountTabelView.delegate = self;
    _discountTabelView.dataSource = self;
    _discountTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    
    
    if (!_favouriteButton) {
        _favouriteButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
        if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0f) {
            _favouriteButton.frame = CGRectMake(20, 0, 44, 44);
        }
        _favouriteButton.backgroundColor = [UIColor clearColor];
        NSString *sharePath = [resourcePath stringByAppendingPathComponent:kFavouriteImageFile];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:sharePath] forState:UIControlStateNormal];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:sharePath] forState:UIControlStateSelected];
        [_favouriteButton addTarget:self action:@selector(favouriteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:_favouriteButton];
    
    UIImageView *bottomBGImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, rect.size.height, 320, 46)];
    bottomBGImageView.backgroundColor = [UIColor clearColor];
    NSString *bottomPath = [resourcePath stringByAppendingPathComponent:kBottomBarImageFile];
    [bottomBGImageView setImage:[UIImage imageWithContentsOfFile:bottomPath]];
    
    bottomBGImageView.userInteractionEnabled = YES;
    
    UIButton *signButton = [[UIButton alloc]initWithFrame:CGRectMake(22, 4, 20, 20)];
    signButton.backgroundColor = [UIColor clearColor];
    NSString *signPath = [resourcePath stringByAppendingPathComponent:kSignImageFile];
    [signButton setImage:[UIImage imageWithContentsOfFile:signPath] forState:UIControlStateNormal];
    [signButton setImage:[UIImage imageWithContentsOfFile:signPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:signButton];
    [signButton release];
    
    UILabel *signLabel = [[UILabel alloc]initWithFrame:CGRectMake(13, 27, 50, 11)];
    signLabel.backgroundColor = [UIColor clearColor];
    signLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    signLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    signLabel.text = @"签到留言";
    [bottomBGImageView addSubview:signLabel];
    [signLabel release];
    
    UIButton *signTransButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 46)];
    signTransButton.backgroundColor = [UIColor clearColor];
    [signTransButton addTarget:self action:@selector(signButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:signTransButton];
    [signTransButton release];
    
    
    UIButton *sendPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(112, 4, 20, 20)];
    sendPhotoButton.backgroundColor = [UIColor clearColor];
    NSString *sendPhotoPath = [resourcePath stringByAppendingPathComponent:kSendPhotoImageFile];
    [sendPhotoButton setImage:[UIImage imageWithContentsOfFile:sendPhotoPath] forState:UIControlStateNormal];
    [sendPhotoButton setImage:[UIImage imageWithContentsOfFile:sendPhotoPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:sendPhotoButton];
    [sendPhotoButton release];
    
    UILabel *sendPhotoLabel = [[UILabel alloc]initWithFrame:CGRectMake(101, 27, 50, 11)];
    sendPhotoLabel.backgroundColor = [UIColor clearColor];
    sendPhotoLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    sendPhotoLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    sendPhotoLabel.text = @"上传照片";
    [bottomBGImageView addSubview:sendPhotoLabel];
    [sendPhotoLabel release];
    
    UIButton *sendPhotoTransButton = [[UIButton alloc]initWithFrame:CGRectMake(90, 0, 70, 46)];
    sendPhotoTransButton.backgroundColor = [UIColor clearColor];
    [sendPhotoTransButton addTarget:self action:@selector(sendPhotoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:sendPhotoTransButton];
    [sendPhotoTransButton release];
    
    
    UIButton *dianpingButton = [[UIButton alloc]initWithFrame:CGRectMake(198, 4, 20, 20)];
    dianpingButton.backgroundColor = [UIColor clearColor];
    NSString *dianpingPath = [resourcePath stringByAppendingPathComponent:kDianpingImageFile];
    [dianpingButton setImage:[UIImage imageWithContentsOfFile:dianpingPath] forState:UIControlStateNormal];
    [dianpingButton setImage:[UIImage imageWithContentsOfFile:dianpingPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:dianpingButton];
    [dianpingButton release];
    
    UILabel *dianpingLabel = [[UILabel alloc]initWithFrame:CGRectMake(187, 27, 50, 11)];
    dianpingLabel.backgroundColor = [UIColor clearColor];
    dianpingLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    dianpingLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    dianpingLabel.text = @"添加点评";
    [bottomBGImageView addSubview:dianpingLabel];
    [dianpingLabel release];
    
    UIButton *dianpingTransButton = [[UIButton alloc]initWithFrame:CGRectMake(170, 0, 70, 46)];
    dianpingTransButton.backgroundColor = [UIColor clearColor];
    [dianpingTransButton addTarget:self action:@selector(dianpingButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:dianpingTransButton];
    [dianpingTransButton release];
    
    
    UIButton *wrongButton = [[UIButton alloc]initWithFrame:CGRectMake(280, 4, 20, 20)];
    wrongButton.backgroundColor = [UIColor clearColor];
    NSString *wrongPath = [resourcePath stringByAppendingPathComponent:kWrongImageFile];
    [wrongButton setImage:[UIImage imageWithContentsOfFile:wrongPath] forState:UIControlStateNormal];
    [wrongButton setImage:[UIImage imageWithContentsOfFile:wrongPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:wrongButton];
    [wrongButton release];
    
    UILabel *wrongLabel = [[UILabel alloc]initWithFrame:CGRectMake(279, 27, 50, 11)];
    wrongLabel.backgroundColor = [UIColor clearColor];
    wrongLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    wrongLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    wrongLabel.text = @"报错";
    [bottomBGImageView addSubview:wrongLabel];
    [wrongLabel release];
    
    UIButton *wrongTransButton = [[UIButton alloc]initWithFrame:CGRectMake(250, 0, 70, 46)];
    wrongTransButton.backgroundColor = [UIColor clearColor];
    [wrongTransButton addTarget:self action:@selector(wrongButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:wrongTransButton];
    [wrongTransButton release];
    
    [self.viewContainer addSubview:bottomBGImageView];
    [bottomBGImageView release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

-(void)dianpingButtonClicked{
    
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantDetailWriteCommentViewController *vc = [[RCMerchantDetailWriteCommentViewController alloc]initWithMerchant:self.activityID withName:self.detailDataModel.pictureInfo.title];
    vc.delegate = (id<MerchantWriteCommentDelegate>)self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)signButtonClicked{
    
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantDetailWriteSigninViewController *vc = [[RCMerchantDetailWriteSigninViewController alloc]initWithMerchant:self.detailDataModel.shopInfo.shopID withName:self.detailDataModel.pictureInfo.title];
    vc.delegate = (id<MerchantWriteSigninDelegate>)self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)wrongButtonClicked{
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    RCMerchantCommitWrongInfoViewController *vc = [[RCMerchantCommitWrongInfoViewController alloc]initWithMerchantName:self.detailDataModel.shopInfo.shopID withName:self.detailDataModel.pictureInfo.title];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)sendPhotoButtonClicked{
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantUploadPhotoViewController *vc = [[RCMerchantUploadPhotoViewController alloc]initWithShopID:self.detailDataModel.shopInfo.shopID];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


-(void)favouriteButtonClicked{
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    
    if (self.hasFavourited) {
        _cancelRequestID = [[IManager sharedInstance]requestMerchantDeleteFavourite:self.activityID withDelegate:self];
    }else{
        _favouriterRequestID = [[IManager sharedInstance]requestMerchantAddFavourite:self.activityID withDelegate:self];
    }
}

-(void)createTableHeaderView{
    
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 188)];
        _tableHeaderView.backgroundColor = [UIColor clearColor];
        
        _picScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 188)];
        _picScrollView.backgroundColor = [UIColor clearColor];
        _picScrollView.delegate = self;
        _picScrollView.showsHorizontalScrollIndicator = NO;
        _picScrollView.pagingEnabled = YES;
        
        if (self.detailDataModel.pictureInfo.pictureArray != nil && [self.detailDataModel.pictureInfo.pictureArray count] >0) {
        
            NSInteger count = [self.detailDataModel.pictureInfo.pictureArray count];
            _picScrollView.contentSize = CGSizeMake(320*count, 188);
            
            for (int i = 0; i < count; i++) {
                
                NSString *picUrl = [self.detailDataModel.pictureInfo.pictureArray safeObjectAtIndex:i];
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 188)];
                imageView.backgroundColor = [UIColor clearColor];
                [imageView setImageWithURL:[NSURL URLWithString:picUrl] placeholderImage:nil];
                [_picScrollView addSubview:imageView];
                [imageView release];
            }
            
        }
        
        
        [_tableHeaderView addSubview:_picScrollView];
        
        _picPageControl = [[SMPageControl alloc]init];
        CGFloat pageContWidth = (9+3)*3;
        _picPageControl.frame = CGRectMake((320-pageContWidth)/2, 188-11-9, pageContWidth, 9);
        _picPageControl.indicatorMargin = 3;
        [_picPageControl setUserInteractionEnabled:NO];
        _picPageControl.backgroundColor=[UIColor clearColor];
        _picPageControl.indicatorDiameter = 7;
        _picPageControl.numberOfPages = [self.detailDataModel.pictureInfo.pictureArray count];
        [_picPageControl setPageIndicatorImage:[UIImage imageNamed:kSwitchSceneDotInActiveImage]];
        [_picPageControl setCurrentPageIndicatorImage:[UIImage imageNamed:kSwitchSceneDotActiveImage]];
        _picPageControl.currentPage = 0;
        [_tableHeaderView addSubview:_picPageControl];
    }
    
    
}


#pragma
#pragma mark --- UITableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.detailDataModel) {
        return 1;
    }
    return 0;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger imageCount = 0;
    if (self.detailDataModel != nil && self.detailDataModel.contentDetail != nil && self.detailDataModel.contentDetail.pictureArray != nil) {
        imageCount = [self.detailDataModel.contentDetail.pictureArray count];
    }
    self.cellCount = 4+imageCount;
    
    return self.cellCount;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    
    if (0 == row) {
        return [RCDiscountDetailNewDiscountInfoCell getCellHeight:self.detailDataModel.pictureInfo];
    }else if (1 == row){
        return [RCDiscountDetailNewShopInfoCell getCellHeight:self.detailDataModel.shopInfo];
    }else if (2 == row){
        if (nil == self.detailDataModel || nil == self.detailDataModel.contentDetail || nil == self.detailDataModel.contentDetail.pictureArray || 0 == [self.detailDataModel.contentDetail.pictureArray count]) {
            return 0.0f;
        }
        
        return [RCDiscountDetailNewContentTitleCell getCellHeight];
    }else if (row > 2 && row < self.cellCount-1){
        NSString *imageUrl = [self.detailDataModel.contentDetail.pictureArray safeObjectAtIndex:row-3];
        if ([NSString stringIsNullOrEmpty:imageUrl]) {
            return 0.0f;
        }else{
            return [RCDiscountDetailNewContentImageCell getCellHeight:imageUrl];
        }
    }else if (row == self.cellCount-1){
        return [RCDiscountDetailNewNoticeCell getCellHeight:self.detailDataModel.noticeInfo];
    }
    
    return 0.0;
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    NSString *CellIdentifier = [NSString stringWithFormat:@"DiscountDetailCell%d%d",section,row];
    
    if ( row > 2 && row < self.cellCount-1) {
        
        CellIdentifier = @"ContentCellImage";
    }
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (0 == row) {
        cell = [[[RCDiscountDetailNewDiscountInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        [((RCDiscountDetailNewDiscountInfoCell *)cell) bindData:self.detailDataModel.pictureInfo];
        
    }else if (1 == row){
        cell = [[[RCDiscountDetailNewShopInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        ((RCDiscountDetailNewShopInfoCell *)cell).parentVC = self;
        [((RCDiscountDetailNewShopInfoCell *)cell) bindData:self.detailDataModel.shopInfo];
        
    }else if (2 == row){
        cell = [[[RCDiscountDetailNewContentTitleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        if (nil != self.detailDataModel && nil != self.detailDataModel.contentDetail && nil != self.detailDataModel.contentDetail.pictureArray && 0 != [self.detailDataModel.contentDetail.pictureArray count]) {
            [((RCDiscountDetailNewContentTitleCell *)cell) bindData:self.detailDataModel.contentDetail.content];
        }
        
        
    }else if (row > 2 && row < self.cellCount-1){
        cell = [[[RCDiscountDetailNewContentImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        NSString *imageUrl = [self.detailDataModel.contentDetail.pictureArray safeObjectAtIndex:row - 3];
        [((RCDiscountDetailNewContentImageCell *)cell) bindData:imageUrl];
        
    }else if (row == self.cellCount-1){
        cell = [[[RCDiscountDetailNewNoticeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        if ( self.detailDataModel != nil && self.detailDataModel.noticeInfo != nil && ![NSString stringIsNullOrEmpty:self.detailDataModel.noticeInfo.content]) {
            [((RCDiscountDetailNewNoticeCell *)cell) bindData:self.detailDataModel.noticeInfo];
        }
        
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 55;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 55)]autorelease];
    headerView.backgroundColor = [UIColor colorWithHex:0xfff4f4f4];
    
    
    CGFloat width = 20.0;
    CGFloat height = 0.0f;
    if (self.detailDataModel.pictureInfo.activityPresentPrice > 0) {
        
        UILabel *originPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 14, 100, 23)];
        originPriceLabel.backgroundColor = [UIColor clearColor];
        originPriceLabel.textColor = [UIColor colorWithHex:0xffff8b00];
        originPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:23];
        [headerView addSubview:originPriceLabel];
        
        originPriceLabel.text = [NSString stringWithFormat:@"￥%d",self.detailDataModel.pictureInfo.activityPresentPrice];
        CGSize size = [originPriceLabel.text sizeWithFont:originPriceLabel.font constrainedToSize:CGSizeMake(200, 50)];
        CGRect rect = originPriceLabel.frame;
        rect.size.width = size.width;
        rect.origin.y = height+14;
        originPriceLabel.frame = rect;
        width = CGRectGetMaxX(originPriceLabel.frame);
        [originPriceLabel release];
    }
    
    
    if (self.detailDataModel.pictureInfo.activityOriginPrice > 0) {
        
        UILabel *presentPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(140, 18, 60, 16)];
        presentPriceLabel.backgroundColor = [UIColor clearColor];
        presentPriceLabel.textColor = [UIColor colorWithHex:0xff9b9b9b];
        presentPriceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
        [headerView addSubview:presentPriceLabel];
        
        UIView *priceLineImageView = [[UIView alloc]initWithFrame:CGRectMake(135, 216, 70, 1)];
        priceLineImageView.backgroundColor = [UIColor colorWithHex:0xff9b9b9b];
        [headerView addSubview:priceLineImageView];
        
        
        presentPriceLabel.text = [NSString stringWithFormat:@"价值￥%d",self.detailDataModel.pictureInfo.activityOriginPrice];
        
        CGSize size = [presentPriceLabel.text sizeWithFont:presentPriceLabel.font constrainedToSize:CGSizeMake(200, 50)];
        CGRect rect = presentPriceLabel.frame;
        rect.origin.x = width + 10;
        rect.size.width = size.width;
        rect.origin.y = height + 18;
        presentPriceLabel.frame = rect;
        
        priceLineImageView.frame = CGRectMake(width+5, height+26, size.width+10, 1);
        
        [priceLineImageView release];
        [presentPriceLabel release];
    }
    
    UIButton *baomingImageView = [[UIButton alloc]initWithFrame:CGRectMake(210, 12, 96, 31)];
    baomingImageView.backgroundColor = [UIColor clearColor];
    [baomingImageView setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBaomingImageFile ofType:Nil]] forState:UIControlStateNormal];
    [baomingImageView setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBaomingImageFile ofType:Nil]] forState:UIControlStateHighlighted];
    [baomingImageView addTarget:self action:@selector(gotoOrderDiscountVC) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:baomingImageView];
    
    UIImageView *lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 54, 320, 1)];
    lineImageView.backgroundColor = [UIColor clearColor];
    [lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:Nil]]];
    [headerView addSubview:lineImageView];
    baomingImageView.frame = CGRectMake(210, height + 12, 96, 31);
    lineImageView.frame = CGRectMake(0, height + 54, 320, 1);
    
    [lineImageView release];
    [baomingImageView release];
    
    return headerView;
}

#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance]requestActivityDetail:self.activityID withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        self.detailDataModel = (RCActivityDetailDataModel *)dataModel;
        
        
        self.hasFavourited = self.detailDataModel.shopInfo.favourited;
        NSString *filePath = nil;
        
        if (self.hasFavourited) {
            filePath = [[NSBundle mainBundle] pathForResource:kFavouritedImageFile ofType:nil];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
        }else{
            filePath = [[NSBundle mainBundle] pathForResource:kFavouriteImageFile ofType:nil];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
        }
        
        if (self.detailDataModel && self.detailDataModel.pictureInfo && self.detailDataModel.pictureInfo.pictureArray && [self.detailDataModel.pictureInfo.pictureArray count] > 0) {
            
            [self createTableHeaderView];
            _discountTabelView.tableHeaderView = _tableHeaderView;
        }else{
            _discountTabelView.tableHeaderView = nil;
        }
        [_discountTabelView reloadData];
        
    }else if (_favouriterRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"收藏成功"];
        self.hasFavourited = YES;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:kFavouritedImageFile ofType:nil];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
    }else if (_cancelRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"取消收藏成功"];
        self.hasFavourited = NO;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:kFavouriteImageFile ofType:nil];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
    }
    
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }else if (_favouriterRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"收藏失败"];
        
    }else if (_cancelRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"取消收藏失败"];
        
    }
}

-(void)gotoOrderDiscountVC{
    RCOrderVC *vc = [[RCOrderVC alloc]initWithShopID:self.detailDataModel.shopInfo.shopID];
    vc.activityID = self.activityID;
    vc.pageType = OrderPageTypeActivity;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];

}

#pragma mark -

#pragma mark UIScrollView Delegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.discountTabelView) {
        return;
    }
    int count = _picPageControl.numberOfPages;
    if (count == 1) {
        return;
    }
    NSInteger pageNumber = 0;
    pageNumber = floor((scrollView.contentOffset.x - scrollView.frame.size.width/3)/scrollView.frame.size.width)+1;
    
    [_picPageControl setCurrentPage:pageNumber];
    
}


@end
